//
//  YRJCityAllTripValuedViewController.m
//  YanRenjie
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJCityAllTripValuedViewController.h"
#import "YRJTripHeader.h"
#import "YRJDestinationViewController.h"

@interface YRJCityAllTripValuedViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain)UICollectionView *goFreedomCollectionView;
@property(nonatomic, retain)UICollectionView *localTouristCollectionView;
@property(nonatomic, retain)UICollectionViewFlowLayout *goFreedomFlowLayout;
@property(nonatomic, retain)UICollectionViewFlowLayout *localTouristFlowLayout;
@property(nonatomic, retain)NSMutableArray *goFreedomArr;
@property(nonatomic, retain)NSMutableArray *localTouristArr;
@property(nonatomic, retain)UIButton *tripValuedButton;
@property(nonatomic, retain)UIButton *localTouristButton;
@property(nonatomic, retain)MBProgressHUD *hud;

@end
NSInteger cityGoFreedomPageIndex = 1;
NSInteger cityLocalTouristPageIndex = 1;
@implementation YRJCityAllTripValuedViewController

- (void)dealloc {
    [_string release];
    [_lat release];
    [_lon release];
    [_scrollView release];
    [_goFreedomCollectionView release];
    [_localTouristCollectionView release];
    [_goFreedomFlowLayout release];
    [_localTouristFlowLayout release];
    [_goFreedomArr release];
    [_localTouristArr release];
    [_hud release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"旅游所值";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStylePlain target:self action:@selector(backHomeAction:)];
    
    // 头部的超值自由行和精彩当地游按钮
    self.tripValuedButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.tripValuedButton];
    self.tripValuedButton.frame = CGRectMake(WIDTH / 4, 0, WIDTH / 4, (30.0 / 667) * HEIGHT);
    [self.tripValuedButton setTitle:@"超值自由行" forState:UIControlStateNormal];
    [self.tripValuedButton addTarget:self action:@selector(tripValuedAction:) forControlEvents:UIControlEventTouchUpInside];
    self.tripValuedButton.backgroundColor = [UIColor cyanColor];
    self.tripValuedButton.layer.borderWidth = 1;
    
    self.localTouristButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.localTouristButton];
    self.localTouristButton.frame = CGRectMake(WIDTH / 4 * 2, 0, WIDTH / 4, (30.0 / 667) * HEIGHT);
    [self.localTouristButton setTitle:@"精彩当地游" forState:UIControlStateNormal];
    [self.localTouristButton addTarget:self action:@selector(localTouristAction:) forControlEvents:UIControlEventTouchUpInside];
    self.localTouristButton.layer.borderWidth = 1;
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:
                       CGRectMake(0, (30.0 / 667) * HEIGHT, WIDTH, HEIGHT + 30 + 113 )];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 2, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = YES;
    self.scrollView.delegate = self;
    
    self.goFreedomFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.goFreedomFlowLayout.itemSize = CGSizeMake(WIDTH / 2, HEIGHT / 4);
    self.goFreedomFlowLayout.minimumInteritemSpacing = 0;
    self.goFreedomFlowLayout.minimumLineSpacing = 0;
    
    self.localTouristFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.localTouristFlowLayout.itemSize = CGSizeMake(WIDTH / 2, HEIGHT / 4);
    self.localTouristFlowLayout.minimumInteritemSpacing = 0;
    self.localTouristFlowLayout.minimumLineSpacing = 0;
    
    self.goFreedomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:self.goFreedomFlowLayout];
    [self.scrollView addSubview:self.goFreedomCollectionView];
    self.goFreedomCollectionView.backgroundColor = [UIColor whiteColor];
    [_goFreedomCollectionView release];
    self.goFreedomCollectionView.dataSource = self;
    self.goFreedomCollectionView.delegate = self;
    self.goFreedomCollectionView.tag = 1001;
    [self.goFreedomCollectionView registerClass:[YRJAllTripValuedCollectionViewCell class] forCellWithReuseIdentifier:@"goFreedomReuse"];
    self.goFreedomCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(goFreedomPullDownToRefresh)];
    self.goFreedomCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        cityGoFreedomPageIndex++;
        [self goFreedomPullUpToLoading];
    }];
    
    self.localTouristCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT) collectionViewLayout:self.localTouristFlowLayout];
    [self.scrollView addSubview:self.localTouristCollectionView];
    self.localTouristCollectionView.backgroundColor = [UIColor whiteColor];
    [_localTouristCollectionView release];
    self.localTouristCollectionView.delegate = self;
    self.localTouristCollectionView.dataSource = self;
    self.localTouristCollectionView.tag = 2001;
    [self.localTouristCollectionView registerClass:[YRJLocalTouristCollectionViewCell class] forCellWithReuseIdentifier:@"localTouristReuse"];
    self.localTouristCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(localTouristPullDownToRefresh)];
    self.localTouristCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        cityLocalTouristPageIndex++;
        [self localTouristPullUpToLoading];
    }];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"Loading...";
    
    [self createData];
}


#pragma mark - 超值自由行, 精彩当地游按钮方法
- (void)tripValuedAction:(UIButton *)button {
    button.backgroundColor = [UIColor cyanColor];
    self.localTouristButton.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
}


- (void)localTouristAction:(UIButton *)button {
    button.backgroundColor = [UIColor cyanColor];
    self.tripValuedButton.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentOffset = CGPointMake(0, 0);
}


#pragma mark - 从网络上请求数据
- (void)createData {
    //超值自由行
    NSString *cityGoFreedomURL = [NSString stringWithFormat:@"%@%@%@%ld%@", @"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=", self.string, @"&order=2&page=", cityGoFreedomPageIndex, @"&product_type=1016%2C1018%2C1020&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=AB5B28ED-8670-4190-873A-F09EFE3EB3E8&track_os=ios%25207.1.2&type=2&v=1"];
    
    
    AFHTTPRequestOperationManager *managerOne = [AFHTTPRequestOperationManager manager];
    [managerOne GET:cityGoFreedomURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.goFreedomArr = [YRJGoFreedomModel baseModelByArr:dic[@"data"][@"list"]];
        [self.goFreedomCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    //精彩当地游
    NSString *cityLocalTouristURL = [NSString stringWithFormat:@"%@%@%@%ld%@", @"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=", self.string, @"&order=2&page=", cityLocalTouristPageIndex, @"&product_type=2410&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=AB5B28ED-8670-4190-873A-F09EFE3EB3E8&track_os=ios%25207.1.2&type=2&v=1"];
    
    
    AFHTTPRequestOperationManager *managerTwo = [AFHTTPRequestOperationManager manager];
    [managerTwo GET:cityLocalTouristURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.localTouristArr = [YRJLocalTouristModel baseModelByArr:dic[@"data"][@"list"]];
        [self.localTouristCollectionView reloadData];
        self.hud.hidden = YES;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
   
}


#pragma mark - collectionView 的上拉加载,下拉刷新的方法
- (void)goFreedomPullUpToLoading {
    NSString *goFreedomURL = [NSString stringWithFormat:@"%@%@%@%ld%@", @"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=867739013144832&track_app_version=6.8.1&track_app_channel=hiapk&track_device_info=bbk77_cu_ics2&track_os=Android4.0.4&app_installtime=1445390305001&lat=38.88336&lon=121.544265&type=1&id=", self.string, @"&count=10&page=", cityGoFreedomPageIndex, @"&product_type=1016,1018,1020&time=1&order=2"];
    AFHTTPRequestOperationManager *managerOne = [AFHTTPRequestOperationManager manager];
    [managerOne GET:goFreedomURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *array = [YRJGoFreedomModel baseModelByArr:dic[@"data"][@"list"]];
        for (YRJGoFreedomModel *tempModel in array) {
            [self.goFreedomArr addObject:tempModel];
        }
        [self.goFreedomCollectionView reloadData];
        [self.goFreedomCollectionView.footer endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (void)goFreedomPullDownToRefresh {
    [self.goFreedomArr removeAllObjects];
    [self goFreedomPullUpToLoading];
}


- (void)localTouristPullUpToLoading {
    NSString *localTouristURL = [NSString stringWithFormat:@"%@%@%@%ld%@", @"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=867739013144832&track_app_version=6.8.1&track_app_channel=hiapk&track_device_info=bbk77_cu_ics2&track_os=Android4.0.4&app_installtime=1445390305001&lat=38.88336&lon=121.544265&type=1&id=", self.string, @"&count=10&page=", cityLocalTouristPageIndex, @"&product_type=2410&time=1&order=2"];
    AFHTTPRequestOperationManager *managerTwo = [AFHTTPRequestOperationManager manager];
    [managerTwo GET:localTouristURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *array = [YRJLocalTouristModel baseModelByArr:dic[@"data"][@"list"]];
        for (YRJLocalTouristModel *tempModel in array) {
            [self.localTouristArr addObject:tempModel];
        }
        [self.localTouristCollectionView reloadData];
        [self.localTouristCollectionView.footer endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (void)localTouristPullDownToRefresh {
    [self.localTouristArr removeAllObjects];
    [self localTouristPullUpToLoading];
}


#pragma mark - collectionView 必须实现的两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([collectionView tag] == 1001) {
        return self.goFreedomArr.count;
    } else {
        return self.localTouristArr.count;
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView tag] == 1001) {
        YRJAllTripValuedCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"goFreedomReuse" forIndexPath:indexPath];
        if (self.goFreedomArr.count != 0) {
            [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:[self.goFreedomArr[indexPath.row] photo]] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
            cell.titleLabel.text = [self.goFreedomArr[indexPath.row] title];
            cell.priceoffLabel.text = [self.goFreedomArr[indexPath.row] priceoff];
            cell.priceLabel.text = [[[self.goFreedomArr[indexPath.row] price] stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
        }
        return cell;
    } else {
        YRJLocalTouristCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"localTouristReuse" forIndexPath:indexPath];
        if (self.localTouristArr.count != 0) {
            [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:[self.localTouristArr[indexPath.row] photo]] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
            cell.titleLabel.text = [self.localTouristArr[indexPath.row] title];
            cell.priceoffLabel.text = [self.localTouristArr[indexPath.row] priceoff];
            cell.priceLabel.text = [[[self.localTouristArr[indexPath.row] price] stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
        }
        return cell;
    }
}


#pragma mark - collectionView 的点击方法实现
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView tag] == 1001) {
        YRJGoFreedomAndLocalTouristWebViewController *goFreedomAndLocalTouristVC = [[YRJGoFreedomAndLocalTouristWebViewController alloc] init];
        goFreedomAndLocalTouristVC.idNumber = [self.goFreedomArr[indexPath.row] id];
        [self.navigationController pushViewController:goFreedomAndLocalTouristVC animated:YES];
        [goFreedomAndLocalTouristVC release];
    } else {
        YRJGoFreedomAndLocalTouristWebViewController *goFreedomAndLocalTouristVC = [[YRJGoFreedomAndLocalTouristWebViewController alloc] init];
        goFreedomAndLocalTouristVC.idNumber = [self.localTouristArr[indexPath.row] id];
        [self.navigationController pushViewController:goFreedomAndLocalTouristVC animated:YES];
        [goFreedomAndLocalTouristVC release];
    }
}


#pragma mark - scrollView 的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        self.tripValuedButton.backgroundColor = [UIColor cyanColor];
        self.localTouristButton.backgroundColor = [UIColor whiteColor];
    } else {
        self.tripValuedButton.backgroundColor = [UIColor whiteColor];
        self.localTouristButton.backgroundColor = [UIColor cyanColor];
    }
}


#pragma mark - 返回首页操作
- (void)backHomeAction:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
